<?php
/**
 * Erstellt und verwaltet den Kategoriecontent
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 *
 * */


Class Yapary_Page_ContentList
{
    /** @var $oSql Yapary_Database_Sql */
    private $oSql           = NULL;
    /** @var $dbTableName string */
    private $dbTableName    = 'yapary_page_content';


    /**
     * @param Object $_oSql	: Datenbankklasse fuer Datenbankzugriff
     * */
    function __construct (Yapary_Db_Sql_Connector $_oSql)
    {
		if (!$_oSql->database()->table($this->dbTableName)->exists()) {
            $e = new Exception();
            $arrTrace = $e->getTrace();
            $trace = '<b>'.$arrTrace[0]['class'].$arrTrace[0]['type'].$arrTrace[0]['function'].'</b> in <b>'.$arrTrace[0]['file'].'</b> on line <b>'.$arrTrace[0]['line'].'</b>';
            trigger_error('<pre></b>No database installed<br />'.$trace.'</pre>', E_USER_WARNING);
		}

		$this->oSql  = $_oSql;
    }


    /**
     * Fuegt einen neuen Datensatz hinzu
     * @return Yapary_Page_Content|NULL
     * */
    public function add ($_id, $_name, $_content)
    {
        $oDbResult = $this->oSql->request('INSERT INTO '.$this->dbTableName.'(PageId, Name, Content) VALUES (:id, :name, :content)',
                                 array('id'      => $_id,
                                       'name'    => $_name,
                                       'content' => $_content));

        return new Yapary_Page_Content($this->oSql, $oDbResult->getLastInsertId());
    }


    /**
     * Loescht den bestehenden Datensatz der Seite
     * @return bool
     * */
    public function deleteElement ($_id, $_name)
    {
        $oDbResult = $this->oSql->request('DELETE
                					  FROM '.$this->dbTableName.'
                					  WHERE PageId=:id AND Name=:name',
                                    array('id'   => $_id,
                                    	  'name' => $_name));
        return true;
    }


    /**
     * Loescht alle Seiteneintraege
     * @return bool
     * */
    public function delete ($id)
    {
        $oDbResult = $this->oSql->request('DELETE
                					  FROM '.$this->dbTableName.'
                					  WHERE PageId=:id',
                                    array('id' => $id));
        return true;
    }


    /**
     * Liefert einen Datensatz
     * @return Array[Yapary_Page_Content]
     * */
    public function get ($_id)
    {
        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE PageId=:id',
                            array('id' => $_id));

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_Page_Content($this->oSql, $row);
            }

            return $arrResult;
        }

        return NULL;
    }


    /**
     * Liefert einen Datensatz
     * @return Yapary_Page_Content
     * */
    public function getElement ($_id, $_name)
    {
        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE PageId=:id AND Name=:name',
                            array('id'   => $_id,
                                  'name' => $_name));

        if ($oDbResult->getCount() == 1) {
            $row = $oDbResult->getNext();
            return new Yapary_Page_Content($this->oSql, $row);
        }

        return NULL;
    }


    /**
     * Prueft ob der Content existiert
     * @param int $_id	: zu pruefende Id
     * @return Yapary_Page_Content
     * */
    public function exists ($_id)
    {
        $oDbResult = $this->oSql->request('SELECT Id
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Id=:id',
                            array('id' => $_id));

        if ($oDbResult->getCount() == 1) {
            return true;
        }

        return false;
    }


    /**
     * Prueft ob der Content existiert
     * @param int $_pageId	: zu pruefende Seiten-Id
     * qparam string $_contentName : Name des Contents der Seite
     * @return Yapary_Page_Content
     * */
    public function existsPage ($_pageId, $_contentName)
    {
        $oDbResult = $this->oSql->request('SELECT Id
        					  FROM '.$this->dbTableName.' t1
        					  WHERE PageId=:pageId AND Name=:name',
                            array('pageId' => $_pageId,
                                  'name'   => $_contentName));

        if ($oDbResult->getCount() == 1) {
            return true;
        }

        return false;
    }


}


?>